package com.jxt.app.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.jxt.app.entity.base.Apgroup;
import com.jxt.app.entity.base.ApgroupExample;
import com.jxt.app.entity.base.Apnews;
import com.jxt.app.entity.base.ApnewsExample;
import com.jxt.app.entity.base.Apschoolimg;
import com.jxt.app.entity.base.ApschoolimgExample;
import com.jxt.app.service.ApSchoolImgService;
import com.jxt.app.service.ApgroupService;
import com.jxt.app.service.NewsService;
import com.jxt.app.util.Page;

@Controller
public class NewsController {
	@Resource
	private NewsService newsService;
	@Resource
	private ApSchoolImgService apSchoolImgService;
	
	@Resource
	private ApgroupService apgroupService;
	
	
	
	/**
	 * 学校介绍及新闻列表综合 接口 
	 * @param page
	 * @param pageSize
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/school/list/{schoolid}",method=RequestMethod.GET) 
	public String schoolinfo(@PathVariable int schoolid,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
		Map resultMap=new HashMap<Object, Object>();
		ApnewsExample example=new ApnewsExample();
		example.setId(0);
		example.setId_Indicator(ApnewsExample.EXAMPLE_GREATER_THAN);
		
		example.setSchoolid(schoolid);
		example.setSchoolid_Indicator(ApnewsExample.EXAMPLE_EQUALS);
		example.setType((byte)0);
		example.setType_Indicator(ApnewsExample.EXAMPLE_EQUALS);
		Page apnewsList = null;
		try {
			apnewsList = newsService.findListByExample(example, 1, 10);
//			ApgroupExample groupExmaple=new  ApgroupExample();
//			groupExmaple.setId(schoolid);
//			groupExmaple.setId_Indicator(ApgroupExample.EXAMPLE_EQUALS);
			Apgroup group=apgroupService.getModelById(schoolid);
		example.setType((byte)1);
		example.setType_Indicator(ApnewsExample.EXAMPLE_EQUALS);
		
		List<?> intro=newsService.listModel(example);
		
		ApschoolimgExample modelexample=new ApschoolimgExample();
		modelexample.setSchoolid(schoolid);
		modelexample.setSchoolid_Indicator(ApschoolimgExample.EXAMPLE_EQUALS);
		List<Apschoolimg> apschoolimgList=apSchoolImgService.listModel(modelexample);
		

		resultMap.put("code", "1001");
		resultMap.put("school", apnewsList);
		resultMap.put("group", group);
		resultMap.put("newslist", apnewsList);
		resultMap.put("imglist", apschoolimgList);
		if(intro.size()>0){
			resultMap.put("intro", intro.get(0));
		}else{	
			resultMap.put("intro",null);
		}
		
		} catch (Exception e) {
			resultMap.put("code", "-1001");
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("result", resultMap);
		return "newsList";
	}
	
	
	
	/**
	 * 新闻列表分页接口
	 * @param page
	 * @param pageSize
	 * @param requset
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value="/app/news/list/{shcoolid}/{page}/{pageSize}",method=RequestMethod.GET) 
	public String newsList(@PathVariable int page,@PathVariable int shcoolid,@PathVariable int pageSize,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
	
		ApnewsExample example=new ApnewsExample();
		example.setId(0);
		example.setId_Indicator(ApnewsExample.EXAMPLE_GREATER_THAN);
		
		example.setSchoolid(shcoolid);
		example.setSchoolid_Indicator(ApnewsExample.EXAMPLE_EQUALS);
		Page apnewsList = null;
		try {
			apnewsList = newsService.findListByExample(example, page, pageSize);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map resultMap=new HashMap();
		resultMap.put("code", "1001");
		resultMap.put("list", apnewsList);
		model.addAttribute("result", resultMap);
		return "newsList";
	}
	
	
	@RequestMapping(value="/app/news/detail/{id}",method=RequestMethod.GET) 
	public String newsDet(@PathVariable int id,HttpServletRequest requset, Model model) throws UnsupportedEncodingException{
	
		ApnewsExample example=new ApnewsExample();
		example.setId(id);
		example.setId_Indicator(ApnewsExample.EXAMPLE_EQUALS);
		List<Apnews> apnewsList = null;
		try {
			apnewsList = newsService.listModel(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Map resultMap=new HashMap();
		
		if(null!=apnewsList){
			if(apnewsList.size()>0){
				resultMap.put("code", "1001");//有新闻
				resultMap.put("list", apnewsList.get(0));
			}else{
				resultMap.put("code", "1000");//记录为NULL
			}
		}else{
			resultMap.put("code", "-1001");//记录为NULL
		}
		model.addAttribute("result", resultMap);
		return "newsList";
	}

}
